from google.appengine.ext import webapp
from google.appengine.ext import db

from utils import *
from datatypes import *
from CONFIG import *

"""
Handles requests for /createdirectory. Creates directories.
"""
class CreateDirectoryHandler(webapp.RequestHandler):

    def get(self):
        
        # Get the directory name
        dirname = self.request.get("directory_name")
        if dirname == "":
            self.redirect(GAPYCDN_CONFIG["base_url"] + "manage")
            return
        
        # Create the new directory
        newdir = Directory()
        newdir.name = dirname

        # Was a parent directory supplied?
        parent_dir_id = self.request.get("directory_id")
        if parent_dir_id == "":
            parent_dir = Directory.get_root()
        else:
            parent_dir = Directory.get_by_id(int(parent_dir_id))

        # Set the parnent directory
        newdir.parent_directory = parent_dir
        
        # Save!
        newdir.put()

        # Rediriect to the management page
        self.redirect(GAPYCDN_CONFIG["base_url"] + "manage?dir=" + str(newdir.key().id()))
